﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
#region Version Info
/* ========================================================================
* 【本页面功能概述】
*
* 作者：wangjianfeng 创建时间：2012/5/24 14:56:15
* 文件名：ObjectExtension.cs
*
* 修改者： 时间：
* 修改说明：
* ========================================================================
*/
#endregion
namespace Utils.Extention
{
    /// <summary>
    /// object类的扩展方法
    /// </summary>
    public static class ObjectExtension
    {
        /// <summary>
        /// 扩展方法，如果对象为空，则返回空串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static string ToString(object obj)
        {
            return obj == null ? string.Empty : obj.ToString();
        }

        /// <summary>
        /// 扩展方法，如果对象为空，则返回空串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToTrim(this object obj)
        {
            return obj == null ? string.Empty : obj.ToString().Trim();
        }

        /// <summary>
        /// 扩展方法，如果对象为空，则返回0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ToInt32(this object obj)
        {
            int res;
            int.TryParse(ToString(obj), out res);
            return res;
        }

        /// <summary>
        /// 扩展方法，如果对象为空，则返回0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long ToInt64(this object obj)
        {
            long res;
            long.TryParse(ToString(obj), out res);
            return res;
        }

        /// <summary>
        /// 扩展方法，如果对象为空，则返回0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static short ToInt16(this object obj)
        {
            short res;
            short.TryParse(ToString(obj), out res);
            return res;
        }

        /// <summary>
        /// 扩展方法，如果对象为空，则返回0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static double ToDouble(this object obj)
        {
            double res;
            double.TryParse(ToString(obj), out res);
            return res;
        }

        /// <summary>
        /// 扩展方法，如果对象为空，则返回0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static float ToFloat(this object obj)
        {
            float res;
            float.TryParse(ToString(obj), out res);
            return res;
        }

        /// <summary>
        /// 扩展方法，如果对象为空，则返回0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this object obj)
        {
            decimal res;
            decimal.TryParse(ToString(obj), out res);
            return res;
        }

        /// <summary>
        /// 扩展方法，如果对象为空，则返回0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ToBoolean(this object obj)
        {
            bool res;
            bool.TryParse(ToString(obj), out res);
            return res;
        }


        /// <summary>
        /// 扩展方法，如果对象为空，则返回0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this object obj)
        {
            DateTime res;
            DateTime.TryParse(ToString(obj), out res);
            return res;
        }

        /// <summary>
        /// 判断对象是否为空！
        /// </summary>
        /// <param name="obj"></param>
        /// <returns>为空则返回true,否则则返回false</returns>
        public static bool IsNull(this object obj)
        {
            return obj == null;
        }

        /// <summary>
        /// 判断DataSet是否为空
        /// </summary>
        /// <param name="ds"></param>
        /// <returns>为空则返回True，否则返回false</returns>
        public static bool IsNullOrEmpty(this DataSet ds)
        {
            if (ds == null || ds.Tables.Count<1)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 判断DataTable是否为空
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>为空则返回True，否则则返回false</returns>
        public static bool IsNullOrEmpty(this DataTable dt)
        {
            if (dt == null || dt.Rows.Count < 1)
            {
                return true;
            }
            return false;
        }
    }
}
